/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.utils;

import cn.ac.nercis.pes.common.constant.common.BaseConstants;
import cn.ac.nercis.pes.community.component.dialog.MessageDialog;
import cn.ac.nercis.pes.community.component.dialog.MessageType;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * 系统工具类
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public final class SystemUtils {
    private static File recentDirectory = null;
    private final static String OS_NAME_PROPERTY = "os.name";
    private final static String WINDOW_OS_TAG = "Windows";
    private final static String CONFIG_FILE_NAME = "temp.config";

    private SystemUtils(){}

    public static File getRecentDirectory() {
        return recentDirectory;
    }
    public static void setRecentDirectory(final File recentFile){
        recentDirectory = recentFile;
    }
    public static void initFileChooserDirectory(@NonNull FileChooser fileChooser){
        if(Objects.nonNull(recentDirectory)){
            fileChooser.setInitialDirectory(recentDirectory);
        }
    }

    public static String getHomeDirectory(){
        return FileUtils.getFile(System.getProperty("user.home"),"Desktop").getPath();
    }

    public static boolean isWindowOs(){
        return StringUtils.startsWithIgnoreCase(System.getProperty(OS_NAME_PROPERTY),WINDOW_OS_TAG);
    }

    public static void showMessageDialog(String msg, MessageType type, StackPane container){
        MessageDialog.builder()
                .withType(type)
                .withMessage(msg)
                .withContainer(container)
                .build()
                .show();
    }

    public static void openFile(String filePath){
        try {
            if (SystemUtils.isWindowOs()) {
                Runtime.getRuntime().exec("explorer /select,".concat(filePath));
            }
        }catch (IOException ex){
            log.warn("打开文件失败：",ex);
        }
    }


}